package com.wanchuang.qhbb.module.machines;

/**
 * 文件名 : EaringDetailFragment
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/28 20:47
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wanchuang.qhbb.R;
import com.wanchuang.qhbb.adapter.ChuRuBean;
import com.wanchuang.qhbb.adapter.ChuRuNewAdapter;
import com.wanchuang.qhbb.base.BaseFragment;
import com.wanchuang.qhbb.net.Apis;
import com.wanchuang.qhbb.net.NetworkManager;
import com.wanchuang.qhbb.net.WebApis;
import com.wanchuang.qhbb.net.interfaces.OnRequestCallBack;
import com.wanchuang.qhbb.proto.Mystock;
import com.wanchuang.qhbb.web.ui.WebViewActivity;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class ChuRuRecordFragment extends BaseFragment {

    Unbinder unbinder;
    @BindView(R.id.fragment_recycleView)
    RecyclerView fragmentRecycleView;


    private int page = 1; // 当前页数
    private int position;  // 当前viewPage的位置 0:提现消息 1:奖励消息 2:担保消息 3:系统消息
    private List<ChuRuBean> adapterDates = new ArrayList<>(); // 提现消息
    private ChuRuNewAdapter mAdapter; // 提现消息适配器
    private String type;
    private int pagesize = 10;

    public static ChuRuRecordFragment NewInstance(int instanceType) {
        Bundle bundle = new Bundle();
        bundle.putInt("position", instanceType);
        ChuRuRecordFragment messageTypeFragment = new ChuRuRecordFragment();
        messageTypeFragment.setArguments(bundle);
        return messageTypeFragment;
    }


    /**
     * 获取布局文件
     *
     * @return
     */
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_recycleview;
    }


    /**
     * 初始化操作步骤
     */
    @Override
    protected void init() {
        unbinder = ButterKnife.bind(this, rootView);
        position = getArguments().getInt("position");
    }

    /**
     * 初始化界面
     */
    @Override
    protected void initView() {
        initRecycle();
        loadData();
    }

    /**
     * 加载数据
     */
    public void loadData() {
        Mystock.allocationLogList.Builder builder = Mystock.allocationLogList.newBuilder();
        builder.setType(String.valueOf(position));
        NetworkManager.INSTANCE.post(Apis.allocationLogList, builder.build().toByteArray(), page, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] bytes) throws InvalidProtocolBufferException {
                List<Mystock.allocationLogList.List> lists = Mystock.allocationLogList.parseFrom(bytes).getListList();
                String titles = "";// 设置默认title
                for (Mystock.allocationLogList.List item : lists) {
//                    String times = item.getEquipmentTime().substring(0, 7);
//                    if (!times.equals(titles))
//                    {
//                        titles = times;
//                        ChuRuBean titleBean = new ChuRuBean();
//                        titleBean.setTitle(titles);
//                        mAdapter.addData(titleBean);
//                    }
                    ChuRuBean itemBean = new ChuRuBean();
                    itemBean.setItem(item);
                    itemBean.setItemType(1);
                    mAdapter.addData(itemBean);
                }
                int size = lists.size();
                if (size < 10) {
                    mAdapter.getLoadMoreModule().loadMoreEnd(true);  // 没有分页操作
                } else {
                    mAdapter.getLoadMoreModule().loadMoreComplete();  // 没有分页操作
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                if (!TextUtils.isEmpty(errorMessage)) showToast(errorMessage);
            }
        });

    }


    /**
     * 实例化线性底部RecyclerView
     * *
     */
    private void initRecycle() {
        fragmentRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        fragmentRecycleView.setAdapter(mAdapter = new ChuRuNewAdapter(mContext, adapterDates));
        // 每个item的点击事件
        mAdapter.setEmptyView(R.layout.recycle_empty);
//        mAdapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
//            page = mAdapter.getData().size() / pagesize + 1;
//            loadData();
//        });
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (adapterDates.get(position).getItemType() == 1 )
                WebViewActivity.startActivity(mContext, WebApis.Implements_detail + "?id=" + adapterDates.get(position).getItem().getId() + "&type=" + position);
        });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }
}
